﻿var net = require('net');
var arguments = process.argv.splice(2);
var proxyhost = arguments[0]; //被代理的服务的IP
var proxyport = parseInt(arguments[1]); //被代理的端口
var listenport = parseInt(arguments[2]); //代理端口
console.log("被代理的服务的IP:"+proxyhost );
console.log("被代理的端口:"+proxyport);
console.log("代理端口:"+listenport);
net.createServer(function (socket) {
    socket.on("connect", function () {
        console.log((new Date()).toString()+":客户端已连接.....");
        try {
            var db = net.createConnection(proxyport, proxyhost);
            db.on("connect", function () {
                console.log((new Date()).toString() + ":代理服务器连接成功.....");
            });
            var oldAddress = null;
            var getAddress = null;
            var isWsdl = false;
            var newAddress = proxyport == 80 ? proxyhost : proxyhost + ":" + proxyport;
            socket.on("data", function (data) {
                var str = data.toString();
                var regex = new RegExp("[GET|POST][ ]+([\\S]+)[ ]+HTTP", "i");
                var match = str.match(regex);
                if (match != null) {
                    getAddress = match != null ? match[1] : null;
                    isWsdl = getAddress == null ? false : getAddress.lastIndexOf("?wsdl") > 0;
                    regex = new RegExp("(Host:[ ]+)([\\S]+)", "im");
                    match = str.match(regex);
                    oldAddress = match != null ? match[2] : null;
                    regex = new RegExp("(Host:[\\s]+)[\\S]+", "i");
                    str = str.replace(regex, "$1" + proxyhost + ":" + proxyport.toString());
                    regex = new RegExp("Accept-Encoding:[^\\n]+?\\n", "i");
                    str = str.replace(regex, "");
                    db.write(str);
                }
                else {
                    db.write(data);
                }
            });
            db.on("data", function (data) {
                if (isWsdl) {
                    var html = data.toString();
                    var sumLen = (newAddress.length - oldAddress.length) * 3;
                    var regex = new RegExp("(Content-Length:[\\s]+)([\\d]+)", "i");
                    var match = html.match(regex);
                    if (match != null) {
                        var length = parseInt(match[2]);
                        length = length - sumLen;
                        html = html.replace(regex, "$1" + length.toString());
                    }
                    regex = new RegExp(newAddress, "g");
                    html = html.replace(regex, oldAddress);
                    socket.write(html);
                }
                else {
                    socket.write(data);
                }
            });
            db.on("error", function (data) {
                console.log((new Date()).toString() + ":出现错误" + "error:\r\n" + data);
            });
            db.on("end", function () {
                console.log((new Date()).toString() + ":代理请求关闭.");
                socket.destroy();
            });
            socket.on("close", function () {
                console.log((new Date()).toString() + ":客户端请求关闭.");
                db.destroy();
            });

        } catch (err) {
            console.log(err);
        }
    });

}).listen(listenport, "0.0.0.0");
console.log('服务开始运行,请勿关闭: http://127.0.0.1:'+listenport +"/");

